'use strict';

const Controller = require('egg').Controller;
const fs = require('fs')
class UserController extends Controller {
	async filesList() {
		const { ctx } = this;
		// 获取用户id
		const { id } = ctx.state.userinfo.userInfo;
		// 查询用户操作的文件列表
		const result = await ctx.service.fileList.filesList({ user_id: id });
		// 返回
		ctx.body = {
			code: 200,
			data: result,
			message: '列表请求成功'
		}
	}
	async deleteFile() {
		const { ctx } = this;
		const { id } = ctx.params;
		const { originName, path, oss } = await this.service.fileList.findFileByID(id);
		try {
			if (oss) {
				await ctx.oss.delete(originName)
			}
			if (path) {
				await fs.promises.unlink(path)
			}
			await this.service.fileList.deleteFileByID(id)
		} catch (e) {
			console.log(e)
		}

		ctx.body = {
			code: 200,
			message: '删除成功'
		}
	}
}

module.exports = UserController;
